<?php
/**
 * Created by PhpStorm.
 * User: 陈艺赫
 * Date: 2018/5/12
 * Time: 1:33
 */

namespace app\admin\controller;

use app\user\model\Group;
use app\user\model\Menu;
use app\user\model\User;
use think\Db;

////use think\Session;
class Api extends Base
{
    /*
     * 返回当前用户所有Menu
     * */
    public function ajaxUserMenu()
    {
        $menu = Menu::getUserMenus(session('id'));
        $return['data'] = $menu;
        return $return;
    }

    /*
     * 返回用户列表所有信息
     * */
    public function ajaxAllUserInfo()
    {
        $page = input('post.page') - 1;
        $limit = input('post.limit');
        $where = input('post.search');
        $users = Db::name('user')
            ->where('nickname', 'like', "%{$where}%")
            ->field('id,uname,nickname,group_ids,last_login_time,last_login_ip,status,create_time')
            ->limit($page * $limit, $limit)
            ->select();
        $count = Db::name('user')
            ->where('nickname', 'like', "%{$where}%")
            ->count();
        $users_data = [];
        foreach ($users as $k => $v) {
            $users_data['data'][$k] = $v;
            $group_info = Group::getGroup($v['id']);
            if (!empty($group_info)) {
                $temp['group_id'] = implode(',', array_column($group_info, 'id'));
                $temp['group_name'] = implode(',', array_column($group_info, 'name'));
                $users_data['data'][$k] = array_merge($users_data['data'][$k], $temp);
            }
        }
        $users_data['code'] = 0;
        $users_data['count'] = $count;
        return $users_data;
    }

    /*
     * 所有用户组信息
     * */
    public function ajaxAllGroup(){
        $page = input('post.page') - 1;
        $limit = input('post.limit');
        $where = input('post.search');
        $groups = Db::name('group')
            ->where('name', 'like', "%{$where}%")
            ->field('id,name,describe,status,create_time')
            ->limit($page * $limit, $limit)
            ->select();
        $count = Db::name('group')
            ->where('name', 'like', "%{$where}%")
            ->count();
        $groups_data['data'] = $groups->toArray();
        $groups_data['code'] = 0;
        $groups_data['count'] = $count;
        return $groups_data;
    }

}